package arithmetic.LeetCode.MinStack155;

import java.util.ArrayList;
import java.util.List;

/**
 * Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
 * <p>
 * push(x) -- Push element x onto stack.
 * pop() -- Removes the element on top of the stack.
 * top() -- Get the top element.
 * getMin() -- Retrieve the minimum element in the stack.
 *
 * @author zhangyanqi
 * @since 1.0 2018/1/6
 */
public class MinStack {

    List<Integer> list;

    /**
     * initialize your data structure here.
     */
    public MinStack() {
        list = new ArrayList<>();
    }

    public void push(int x) {
        list.add(x);
    }

    public void pop() {
        list.remove(list.size() - 1);
        return;
    }

    public int top() {
        return list.get(list.size() - 1);
    }

    public int getMin() {
        int min = Integer.MAX_VALUE;
        for (int a = 0; a < list.size(); a++) {
            Integer integer = list.get(a);
            if (integer < min) {
                min = integer;
            }
        }
        return min;
    }
}
